package superConference;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * A very rough draft of the author class.
 * 
 * @author Gian Bata
 * @version 1.0v
 */
public class Author extends AbstractUser {
	
	/**
	 * The list of all papers.
	 */
	private List<Paper> my_list_of_papers;
	
	/**
	 * The current paper being used.
	 */
	private Paper my_current_paper;
	
	
	/**
	 * Constructs an author.
	 * 
	 * @param the_id The user ID of the author.
	 * @param the_email The email address if the author.
	 * @param the_name The name of the author.
	 */
	public Author(int the_id, String the_email, String the_name) {
		super(the_id, the_email, the_name);

		my_list_of_papers = new ArrayList<Paper>();
	}


	/**
	 * Constructs an author.
	 * 
	 * @param the_id The user ID of the author.
	 * @param the_email The email address if the author.
	 * @param the_name The name of the author.
	 */
	public Author(int the_id, String the_email, String the_name, List<Paper> the_listOfPapers) {
		super(the_id, the_email, the_name);


		this.my_list_of_papers = the_listOfPapers;
	}

	/**
	 * Creates a paper.
	 * 
	 * @param the_file
	 * @return the created paper file.
	 */
	public Paper createPaper(File the_file) {
		File file = the_file;
		PaperStatus status = PaperStatus.SUBMITTED; 
		
		Paper paper = new Paper(this.userID,this.name, status, "Title", file.getName());
		
		return paper;
	}
	
	
	/**
	 * Sets the current paper via GUI that this class is going to use,
	 * then opens the current paper to be viewed in the GUI.
	 * 
	 * @param the_paper
	 * @throws FileNotFoundException
	 */
	public String openPaper(Paper the_paper) throws FileNotFoundException {
		my_current_paper = the_paper;
		File paper_filename = new File(my_current_paper.getFile());
		Scanner scanner = new Scanner(paper_filename);
		StringBuilder sb = new StringBuilder();

		while (scanner.hasNextLine()) {
			sb.append(scanner.nextLine() + "\n");
		}
		
		scanner.close();
		return sb.toString();
	}

	/**
	 * Submits the current paper.
	 */
	public void submitPaper() {
		Paper paper = my_current_paper;
		my_list_of_papers.add(paper);
	}

	/**
	 * Overwrites the old version (the current opened paper) of the paper to a new one.
	 * 
	 * @param the_new_version_paper The new version of the paper that will replace the old(current) paper.
	 */
	public void editPaper(Paper the_new_version_paper) {
		int id_of_old_paper = my_current_paper.getId();
		
		for (int i = 0; i < my_list_of_papers.size(); i++) {
			Paper temp_paper = my_list_of_papers.get(i);
			
			if (temp_paper.getId() == id_of_old_paper) {
				my_list_of_papers.remove(i);
				my_list_of_papers.add(i, the_new_version_paper);
				break;
			}
			
		}
	}

	/**
	 * Returns a string representation list of all papers including their names and ids.
	 * 
	 * @return a string representation.
	 */
	public String visewListOfPapers() {
		Iterator<Paper> iterator = my_list_of_papers.iterator();

		StringBuilder sb = new StringBuilder();

		while (iterator.hasNext()) {
			Paper x = iterator.next();

			sb.append(x.getId() + "\t" + x.getTitle());
			sb.append("\n");
		}

		return sb.toString();
	}
	
	/**
	 * Remove unwanted papers.
	 * 
	 * @param the_paper
	 */
	public void unsubmitPaper(Paper the_paper) {
		my_list_of_papers.remove(the_paper);
	}
	
	/**
	 * Returns a list of all papers the author submitted.
	 * 
	 * @return the list of papers.
	 */
	public List<Paper> getListOfPaper() {
		return my_list_of_papers;
	}
	
	/**
	 * Returns the current paper.
	 * 
	 * @return the current paper.
	 */
	public Paper getCurrentPaper() {
		return my_current_paper;
	}
	
	/**
	 * The paper with title x.
	 * 
	 * @param the_paper_title the name/title of the paper.
	 * @return the title of the paper.
	 */
	public Paper getPaper(String the_paper_title) {	
		Paper paper_to_be_returned = null;
		
		for (int i = 0; i < my_list_of_papers.size(); i++) {
			Paper temp_paper = my_list_of_papers.get(i);
			
			if (temp_paper.getTitle() == the_paper_title) {
				paper_to_be_returned = temp_paper;
				break;
			}
			
		}
		
		return  paper_to_be_returned;
	}

}
